home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / jdk113 / docs / guide / awt / innercla / DialogW2.jav next >
Encoding:
Text File  |  1997-08-16  |  2.5 KB  |  96 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. public class DialogWindow extends Frame 
  5.               implements ActionListener {
  6.     private boolean inAnApplet = true;
  7.     private SimpleDialog dialog;
  8.     private TextArea textArea;
  9.  
  10.     public DialogWindow() {
  11.         textArea = new TextArea(5, 40);
  12.         textArea.setEditable(false);
  13.         add("Center", textArea);
  14.         Button button = new Button("Click to bring up dialog");
  15.     button.addActionListener(this);
  16.         Panel panel = new Panel();
  17.         panel.add(button);
  18.         add("South", panel);
  19.     addWindowListener(new DWAdapter());
  20.     }
  21.  
  22.     class DWAdapter extends WindowAdapter {
  23.         public void windowClosing(WindowEvent event) {
  24.             if (inAnApplet) {
  25.                 dispose();
  26.             } else {
  27.                 System.exit(0);
  28.             }
  29.         }
  30.     }
  31.  
  32.     public void actionPerformed(ActionEvent event) {
  33.         if (dialog == null) {
  34.             dialog = new SimpleDialog(this, "A Simple Dialog");
  35.         }
  36.         dialog.setVisible(true);
  37.     }
  38.  
  39.     public void setText(String text) {
  40.         textArea.append(text + "\n");
  41.     }
  42.  
  43.     public static void main(String args[]) {
  44.         DialogWindow window = new DialogWindow();
  45.         window.inAnApplet = false;
  46.  
  47.         window.setTitle("DialogWindow Application");
  48.         window.pack();
  49.         window.setVisible(true);
  50.     }
  51. }
  52.  
  53. class SimpleDialog extends Dialog implements ActionListener {
  54.     TextField field;
  55.     DialogWindow parent;
  56.     Button setButton;
  57.  
  58.     SimpleDialog(Frame dw, String title) {
  59.         super(dw, title, false);
  60.         parent = (DialogWindow)dw;
  61.  
  62.         //Create middle section.
  63.     Panel p1 = new Panel();
  64.         Label label = new Label("Enter random text here:");
  65.         p1.add(label);
  66.         field = new TextField(40);
  67.     field.addActionListener(this);
  68.     p1.add(field);
  69.         add("Center", p1);
  70.  
  71.         //Create bottom row.
  72.         Panel p2 = new Panel();
  73.         p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
  74.         Button b = new Button("Cancel");
  75.     b.addActionListener(this);
  76.         setButton = new Button("Set");
  77.     setButton.addActionListener(this);
  78.         p2.add(b);
  79.         p2.add(setButton);
  80.         add("South", p2);
  81.  
  82.     //Initialize this dialog to its preferred size.
  83.     pack();
  84.     }
  85.  
  86.     public void actionPerformed(ActionEvent event) {
  87.     Object source = event.getSource();
  88.         if ( (source == setButton)
  89.            | (source == field)) {
  90.             parent.setText(field.getText());
  91.         }
  92.         field.selectAll();
  93.         setVisible(false);
  94.     }
  95. }
  96.